﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Stock_WxStock_Add : System.Web.UI.Page
{
    protected int WxStockKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WxStockKey = string.IsNullOrEmpty(Request["WxStockKey"]) ? 0 : Convert.ToInt32(Request["WxStockKey"]);

        if (!this.IsPostBack)
        {
            IList<Sys_Account> UserList = new Account().GetNotAccount(1);
            foreach (Sys_Account account in UserList)
                txt_MUserKey.Items.Add(new ListItem("◆ " + account.RealName, account.UserKey.ToString()));

            txt_ReceiveTime.Attributes.Add("onclick", "selectDate(this);");

            if (WxStockKey != 0)
            {
                Dat_WxStock wxstock = new WxStock().SelectWxStock(WxStockKey);

                txt_InDanNum.Text = wxstock.InDanNum;
                txt_StockName.Text = wxstock.StockName;
                txt_Spec.Text = wxstock.Spec;
                txt_Mistake.Text = wxstock.Mistake;
                txt_ReceiveTime.Text = WebUtility.DateTimeToString(wxstock.ReceiveTime).Split(' ')[0];
                txt_Price.Text = WebUtility.DecimalToString(wxstock.Price);
                txt_MUserKey.SelectedValue = wxstock.MUserKey.ToString();

                btnSubmit.Text = "修 改";
            }
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime? dt = null;
        Decimal? de = null;

        if (WxStockKey == 0)
        {
            Dat_WxStock wxstock = new Dat_WxStock
            {
                InDanNum = txt_InDanNum.Text.Trim(),
                StockName = txt_StockName.Text.Trim(),
                Spec = txt_Spec.Text.Trim(),
                Mistake = txt_Mistake.Text.Trim(),
                ReceiveTime = txt_ReceiveTime.Text.Trim()==""?dt:Convert.ToDateTime(txt_ReceiveTime.Text.Trim()),
                Price = txt_Price.Text.Trim() == "" ? de : Convert.ToDecimal(txt_Price.Text.Trim()),
                MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                StockStatus = "已入库",
                InUserKey = Convert.ToInt32(Session["UserKey"])
            };

            if (new WxStock().AddWxStock(wxstock))
            {
                WebUtility.JsAlertToGo("报修物品入库成功", "WxStock_Add.aspx?menutype=361");
            }
        }
        else
        {
                Dat_WxStock wxstock = new Dat_WxStock
                {
                    WxStockKey = WxStockKey,
                    InDanNum = txt_InDanNum.Text.Trim(),
                    StockName = txt_StockName.Text.Trim(),
                    Spec = txt_Spec.Text.Trim(),
                    Mistake = txt_Mistake.Text.Trim(),
                    ReceiveTime = txt_ReceiveTime.Text.Trim() == "" ? dt : Convert.ToDateTime(txt_ReceiveTime.Text.Trim()),
                    Price = txt_Price.Text.Trim() == "" ? de : Convert.ToDecimal(txt_Price.Text.Trim()),
                    MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                    StockStatus = "已入库",
                    InUserKey = Convert.ToInt32(Session["UserKey"])
                };

                if (new WxStock().EditWxStock(wxstock))
                {
                    WebUtility.JsAlertToGo("报修物品修改成功", "WxStock.aspx?menutype=362&stockstatus=已入库");
                }
        }
    }

}
